<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use App\Models\Phpwarn;
use App\Models\Area;

use Carbon\Carbon;
use Validator;
use App\Models\WarnOperator;

class RealwarnController extends Controller
{

    
    public function index(Request $request)
    {
        $perPage = $request->input('pageSize',15);
        
        $query = Phpwarn::query();
        
        $did = $request->input('pdi_devid', '');
        if(!empty($did)) {
            $query->where('pdi_devid', '=', trim($did));
        }
        
        $statusType = $request->input('status_type', 'new');

        if($statusType=='new') {
           // $date = date('Y-m-d'); ->where('pdi_updatetime', '>', $date)
            $query->where('oper_status', '1');
        }

        if($statusType=='has_confirm') {
            $query->where('oper_status', '2');
        }

        $createdAt = $this->formatArrTime($request->input('pdi_updatetime', ''));
        if (!empty($createdAt)) {
            $query->whereBetween('pdi_updatetime', $createdAt);
        }
        
        $wid = $request->input('pdi_warnId', '');
        if (!empty($wid)) {
            $query->whereBetween('pdi_warnId', $wid);
        }

        $pdiName = $request->input('pdi_name', '');
        if (!empty($pdiName)) {
            $query->where('pdi_name', 'like', $pdiName.'%');
        }
        

        $cid = $request->input('company', '');
        $sid = $request->input('site', '');

              // var_dump($query);
        $query->with(['area'=>function($query){
            $query->select(['AreaId', 'AreaName', 'area_text']);
        }]);

        $warns = $query->company($cid)->site($sid)->orderBy('pdi_updatetime', 'desc') -> paginate($perPage);


        
        return $this->success('', ['status' => 1, 'data'=>$warns, 'icons'=>config('device.monitor'), 'wlevel'=>config('device.warn_level')]);
    }
    
    /**
     * 解析时间参数
     * @param Request $request
     * @return type
     */
    protected function getTime(Request $request) {
        $sdate = $request->input('searchDate', []);
        if($sdate) {
            $sdate = json_decode($sdate, true);
        }
        $msg = [
            'start.date' => '开始时间非法',
            'end.date' => '结束时间非法',
        ];
        $v = Validator::make($sdate,[
            'start'=>'date',
            'end'=>'date',
        ],$msg);


        if ($v->fails()) {
            $this->throwValidationException($request, $v);
        }
        $start = '';
        $end = '';
        if(array_get($sdate, 'start') && array_get($sdate, 'end')) {
            $zhStart = Carbon::parse($sdate['start']);
            $start = $zhStart->startOfDay()->toDateTimeString();
            $zhEnd = Carbon::parse($sdate['end']);
            $end = $zhEnd->endOfDay()->toDateTimeString();
            return [$start, $end];
        }
        return [];
    }
    
    //添加操作日志
    public function addWarnLog(Request $request) {
        $post = $request->input();
        $message = [
            'warn_id.required' => '告警编号必须',
            'warn_id.unique' => '告警已阅读',
        ];
        $warnId = array_get($post, 'warn_id');

        Phpwarn::where('pdi_index', $warnId)->where('oper_status', 1)->update(['oper_status'=>2]);

        $count = WarnOperator::where('warn_id', $warnId)->count();
        if(!$count) {
            $this->validate($request,[
                 'warn_id' => 'required',
             ],$message);
            $post['is_read_status'] = 1;
            $post['confirm_user'] = $request->user()->username;
            $post['confirm_userid'] = $request->user()->userid;
            $post['confirm_at'] = date('Y-m-d H:i:s');
            WarnOperator::create($post);
            return $this->success('', [ 'data' => [ 'confirm_user' => $post['confirm_user'], 'confirm_at' => $post['confirm_at'] ],]);
        }

    }
    
    //获取告警未读数量
    public function unreadNum() {
        $num = \App\Models\Phpwarn::doesntHave('warnlog')->count();
        return ['status' => 1, 'data' => $num];
    }
}
